package com.example.spring_project_management.controller;


import com.example.spring_project_management.model.RespBean;
import com.example.spring_project_management.pojo.Asset;
import com.example.spring_project_management.query.AssetQuery;
import com.example.spring_project_management.service.IAssetService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZengQi
 * @since 2021-11-24
 */
@Controller
@RequestMapping("/asset")
public class AssetController {
    @Resource
    private IAssetService assetTableService;

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> assetList(AssetQuery assetQuery) {
        return assetTableService.assetList(assetQuery);
    }

    @RequestMapping("toUpdate")
    public ModelAndView updatePage(Integer id, Model model) {
        if(null != id) {
            model.addAttribute("asset",assetTableService.getById(id));
        }
        return new ModelAndView("/view/asset/asset-update");
    }

    @RequestMapping("delete")
    @ResponseBody
    public RespBean deleteAsset(Integer[] ids) {
        assetTableService.deleteAsset(ids);
        return RespBean.success("用户记录删除成功");
    }

    @RequestMapping("add")
    @ResponseBody
    public RespBean addAsset(Asset asset) {
        assetTableService.addAsset(asset);
        return RespBean.success("用户记录添加成功");
    }

    @RequestMapping("update")
    @ResponseBody
    public RespBean updateAsset(Asset asset) {
        assetTableService.updateAsset(asset);
        return RespBean.success("用户记录更新成功");
    }

}
